import 'package:flutter/services.dart';
import 'package:lib_base/data_repository/data_repository_manager.dart';
import 'package:lib_service/auth_service.dart';
import 'package:lib_service/models/auth_model.dart';
import 'package:lib_service/repositories/auth_repository.dart';
import 'package:rxdart/rxdart.dart';

class AuthServiceImpl implements AuthService {
  final MethodChannel _channel = const MethodChannel('auth-service');

  @override
  AuthRepository authRepository = AuthRepository();

  @override
  BehaviorSubject<bool?> isLogin = BehaviorSubject.seeded(null);

  AuthServiceImpl() {
    DataRepositoryManager.instance
        .register(AuthRepository.kId, () => authRepository);

    authRepository.getData().then((value) => setIsLogin(value));
    authRepository.dataObservable.listen((value) => setIsLogin(value));
  }

  void setIsLogin(AuthModel? authModel) {
    isLogin.add(authModel == null ? false : true);
  }

  @override
  Future<int?> login() {
    return _channel.invokeMethod<int>('login');
  }

  @override
  Future<bool?> logout() async {
    return _channel.invokeMethod<bool>('logout');
  }
}
